package com.skt.common_hr.post.controller
{
	import com.skt.common_hr.common.AppModuleManager;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.post.model.AddressVO;
	import com.skt.common_hr.post.model.PostVO;
	import com.skt.common_hr.post.view.popPostNo;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.utils.ObjectUtil;
	
	public class PostDao
	{
		private static var ins:PostDao;
		private var proxy:CommonProxy;
		
		public static const LOAD_POST_COMPLETE:String 	= "loadPostComplete";
		public static const LOAD_POST_FAILED:String 	= "loadPostFailed";
		public static const LOAD_ADD_COMBOBOX_COMPLETE:String	= "loadAddComboboxComplete";
		public static const LOAD_ADD_COMBOBOX2_COMPLETE:String	= "loadAddCombobox2Complete";
		
		public var addComboboxData:ArrayCollection;
		public var addComboboxData2:ArrayCollection;
		public var postList:ArrayCollection;
		
		public function PostDao()
		{
			proxy = new CommonProxy(AppModuleManager.MODULE_KEY_POSTPOPUP);
		}
		
		public static function getInstance():PostDao
		{
			if(ins==null)ins = new PostDao;
			return ins;	
		}
		
		public function loadAddCombobox(_param:Object):void
		{
			var param:Object = new Object();
			param = _param;
			
			var resultFunc:Function;
			if(param["I_STATE"]==""){
				proxy.exLoadMultiData("Z_HR_0303",param,resultMulti,"addressCombo1");	
			}else{
				proxy.exLoadMultiData("Z_HR_0303",param,resultMulti,"addressCombo2");	
			}
		}
		
		public function loadPost(_param:Object):void{
			var param:Object = _param;
			
			proxy.exLoadMultiData("Z_HR_0009",param,resultMulti,"loadPost");	 
		}
		
		public function saveAddress(_param:AddressVO):void{
			
		}
		
		private function resultMulti(receiver:DataReceiver):void{
			
			if(receiver.retCode == 0){
				if(receiver.type=="addressCombo1"){
					addComboboxData = receiver.result.ITAB;
					popPostNo.ins.addCombo1.dataProvider = addComboboxData;
					popPostNo.ins.addCombo2.enabled = false; 
					popPostNo.ins.addCombo2.selectedIndex = -1;
				}else if(receiver.type=="addressCombo2"){
					addComboboxData2 = receiver.result.ITAB;
					popPostNo.ins.addCombo2.dataProvider = addComboboxData2;
					popPostNo.ins.addCombo2.enabled = true;
				}else if(receiver.type=="loadPost"){
					postList = parsePost(receiver.result.IT_POST);
					popPostNo.ins.dataProvider = postList;					
				}
					
			}else{
				//receiver.retText;
				//receiver.faultDetail;
				CommonAlert.showMessage("", receiver.retText);
			}
		}
		
		private function parsePost(retObj:ArrayCollection):ArrayCollection
		{
			var arr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in retObj)
			{
				var vo:PostVO = new PostVO;
				vo.PSTLZ	= rec["PSTLZ"];
				vo.PFULL	= rec["PFULL"];
				vo.SIDO		= rec["SIDO"];
				vo.STATE	= rec["STATE"];
				vo.GU		= rec["GU"];
				vo.DONG		= StringUtil.NVL(rec["DONG"]);
				if(vo.DONG!="") vo.DONG += " ";
				vo.RI		= StringUtil.NVL(rec["RI"]);
				if(vo.RI!="") vo.RI += " ";
				vo.DOSEO	= StringUtil.NVL(rec["DOSEO"]);
				if(vo.DOSEO!="") vo.DOSEO += " ";
				
				vo.BUNJI	= StringUtil.NVL(rec["BUNJI"]);
				if(vo.BUNJI!="") vo.BUNJI += " ";
				vo.BLDG		= rec["BLDG"];
				if(vo.BLDG!="") vo.BLDG += " ";
				vo.STRAS	= rec["STRAS"];
				if(vo.STRAS!="") vo.STRAS += " ";
				arr.addItem(vo);
			}
			return arr;
		}
		
		
		
		/*
		public function load(_param:Object):void
		{
			var param:Object = new Object();
			param["I_GUBUN"] = _param["I_GUBUN"];
			param["I_STATE"] = _param["I_STATE"];
			param["I_SGGNM"] = _param["I_SGGNM"];
			param["I_TEXT"] = _param["I_TEXT"];
			param["I_TEXT2"] = _param["I_TEXT2"];
			
			proxy.exLoadData("Z_HR_0009", param, result, fault); 
		}
		
		public function result(event:ResultEvent):void
		{
			if(event.result.retCode == 0) {  
				postList = parsePost(event.result.IT_POST);
				facade.sendNotification(LOAD_POST_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_POST_FAILED, this);
			}
		}
		
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_POST_FAILED, this);
		}
		*/
		
		/*
		private function parsePost(retObj:ArrayCollection):ArrayCollection
		{
			var arr:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in retObj)
			{
				var vo:PostVO = new PostVO();
				vo.PSTLZ	= rec["PSTLZ"];
				vo.PFULL	= rec["PFULL"];
				vo.SIDO		= rec["SIDO"];
				vo.STATE	= rec["STATE"];
				vo.GU		= rec["GU"];
				vo.DONG		= StringUtil.NVL(rec["DONG"]);
				if(vo.DONG!="") vo.DONG += " ";
				vo.RI		= StringUtil.NVL(rec["RI"]);
				if(vo.RI!="") vo.RI += " ";
				vo.DOSEO	= StringUtil.NVL(rec["DOSEO"]);
				if(vo.DOSEO!="") vo.DOSEO += " ";
				
				vo.BUNJI	= StringUtil.NVL(rec["BUNJI"]);
				if(vo.BUNJI!="") vo.BUNJI += " ";
				vo.BLDG		= rec["BLDG"];
				if(vo.BLDG!="") vo.BLDG += " ";
				vo.STRAS	= rec["STRAS"];
				if(vo.STRAS!="") vo.STRAS += " ";
				arr.addItem(vo);
			}
			return arr;
		}
		*/
	}
}